package pikes.peak;

import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Anchor;
import pikes.html.xhtml.Image;
import pikes.html.xhtml.Inline;
import pikes.uri.URI;

public class Picture implements InlineComponent {

	private RequestProperty<URI> sourceURI = null;

	private ApplicationContextMessageProvider description = new ApplicationContextMessageProvider("Missing description");

	private RequestProperty<URI> uri = null;

	private RequestProperty<Integer> width = RequestProperty.NULL_VALUE_PROVIDER;

	private RequestProperty<Integer> height = RequestProperty.NULL_VALUE_PROVIDER;

	public RequestProperty<Integer> getWidth() {
		return width;
	}

	public void setWidth(RequestProperty<Integer> width) {
		this.width = width;
	}

	public void setWidth(Integer width) {
		setWidth(new RequestIndependentProperty<Integer>(width));
	}

	public RequestProperty<Integer> getHeight() {
		return height;
	}

	public void setHeight(RequestProperty<Integer> height) {
		this.height = height;
	}

	public void setHeight(Integer height) {
		setHeight(new RequestIndependentProperty<Integer>(height));
	}

	public Picture(RequestProperty<URI> uri) {
		this.sourceURI = uri;
	}

	public Picture(URI uri) {
		this(new RequestIndependentProperty<URI>(uri));
	}

	public Picture(URI uri, String description) {
		this(new RequestIndependentProperty<URI>(uri), description);
	}

	public Picture() {

	}

	public Picture(RequestProperty<URI> uri, String description) {
		this(uri);
		this.description = new ApplicationContextMessageProvider(description);
	}

	public Picture(String description) {
		this.description = new ApplicationContextMessageProvider(description);
	}

	final public Inline createHtml(Map model, RequestContext requestContext) throws Exception {
		Inline result = null;
		Image image = null;
		if (sourceURI == null) {
			image = new Image("#", description.getValue(model, requestContext));
		}
		else {
			image = new Image(sourceURI.getValue(model, requestContext), description.getValue(model, requestContext));
		}

		image.setWidth(width.getValue(model, requestContext));
		image.setHeight(height.getValue(model, requestContext));

		result = image;

		if (uri != null) {
			result = new Anchor(uri.getValue(model, requestContext), result);
		}

		return result;
	}

	public void setURI(RequestProperty<URI> uri) {
		this.uri = uri;
	}

	public void setURI(URI uri) {
		setURI(new RequestIndependentProperty<URI>(uri));
	}

	public RequestProperty<URI> getURI() {
		return uri;
	}

}
